﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChessBoard
{
    class ChessList
    {
        private const int Left = 103;
        private const int Top = 9;
        public Frame ListFrame { get; set; }

        public ChessList()
        {
            this.ListFrame = new Frame()
            {
                Width = 22,
                Height = 52,
                ForegroundColor = ConsoleColor.Gray,
                BackgroundColor = ConsoleColor.Black,
                FrameSymbols = new FrameSymbol()
                {
                    TopLeftCorner = '┌',
                    TopRightCorner = '┐',
                    BottomLeftCorner = '└',
                    BottomRightCorner = '┘',
                    TopHorBorder = '─',
                    BotHorBorder = '─',
                    VertBorder = '│'
                },
            };
        }

        public void Draw(List<string> movesList)
        {
            ConsoleColor fgColor = Console.ForegroundColor;
            Console.BackgroundColor = this.ListFrame.BackgroundColor;
            Console.ForegroundColor = this.ListFrame.ForegroundColor;
            Console.SetCursorPosition(Left + 5, Top);
            ListFrame.Draw(Left, Top + 1);
            Console.SetCursorPosition(Left + 5, Top + 1);
            Console.WriteLine(" MOVE LIST ");
            this.UpdateList(movesList);

            Console.ForegroundColor = fgColor;
        }

        public void UpdateList(List<string> movesList)
        {
            ConsoleColor fgColor = Console.ForegroundColor;
            Console.BackgroundColor = this.ListFrame.BackgroundColor;
            Console.ForegroundColor = this.ListFrame.ForegroundColor; 
            Console.SetCursorPosition(Left + 2, Top + 3);

            int mCount = movesList.Count / 2;
            if (mCount > 0)
            {
                int start = 0;
                if (mCount > 48)
                {
                    start = mCount - 48;
                }
                for (int i = start, j = start; j < mCount; i += 2, j++)
                {
                    Console.CursorLeft = Left + 2;
                    Console.WriteLine("{0,3}. {1}  {2}", (j + 1), movesList[i], movesList[i + 1]);
                }
                if (movesList.Count % 2 != 0)
                {
                    Console.CursorLeft = Left + 2;
                    Console.WriteLine("{0,3}. {1}", ((movesList.Count / 2) + 1), movesList[movesList.Count - 1]);
                }
            }
            else if (movesList.Count == 1)
            {
                Console.CursorLeft = Left + 2;
                Console.WriteLine("{0,3}. {1}", ((movesList.Count / 2) + 1), movesList[movesList.Count / 2]);
            }
            Console.ForegroundColor = fgColor;
        }
    }
}
